package com.zikao.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.zikao.article.entity.TextEntity;
import com.zikao.article.service.TextService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zikao.common.utils.PageUtils;
import com.zikao.common.utils.Query;

import com.zikao.article.dao.ReplyDao;
import com.zikao.article.entity.ReplyEntity;
import com.zikao.article.service.ReplyService;


@Service("replyService")
public class ReplyServiceImpl extends ServiceImpl<ReplyDao, ReplyEntity> implements ReplyService {

    @Autowired
    private TextService textService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ReplyEntity> page = this.page(
                new Query<ReplyEntity>().getPage(params),
                new QueryWrapper<ReplyEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<ReplyEntity> selectBatchByRemarkIds(List<Long> remarkIds) {
        // 使用QueryWrapper实现条件查询
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in("remark_id",remarkIds);
        List<ReplyEntity> replyList = baseMapper.selectList(wrapper);
        List<Long> textIds = replyList.stream().map(r -> {
            return r.getTextId();
        }).collect(Collectors.toList());

        if (textIds.size()>0){
            List<TextEntity> textList = textService.selectBatchIds(textIds);
            Map<Long, TextEntity> textMap = textList.stream().collect(Collectors.toMap(v -> v.getId(), v -> v));
            for (ReplyEntity reply:replyList){
                TextEntity text = textMap.get(reply.getTextId());
                reply.setContent(text.getContent());
            }
        }
        return replyList;
    }
}